package com.fesine.quartz.chapter14;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.*;

/**
 * @description: 类描述
 * @author: Fesine
 * @createTime:2016/11/20 19:48
 * @update:修改内容
 * @author: Fesine
 * @updateTime:2016/11/20 19:48
 */
public class JobChainListener implements JobListener {
    Log logger = LogFactory.getLog(JobChainListener.class);

    public Class nextJobClass;

    public String listenerName;

    public JobChainListener() {
        super();
    }

    public JobChainListener(Class nextJobClass, String listenerName) {
        this.nextJobClass = nextJobClass;
        setName(listenerName);
    }

    public void setName(String name) {
        this.listenerName=name;
    }

    @Override
    public String getName() {
        return listenerName;
    }

    @Override
    public void jobToBeExecuted(JobExecutionContext context) {

    }

    @Override
    public void jobExecutionVetoed(JobExecutionContext context) {

    }

    @Override
    public void jobWasExecuted(JobExecutionContext context, JobExecutionException jobException) {
        try {
            Scheduler scheduler = context.getScheduler();
            JobDetail detail = new JobDetail("ChainedJob", null, nextJobClass);
            Trigger trigger = TriggerUtils.makeImmediateTrigger(0, 5000);
            trigger.setName("FireNowTrigger");
            JobResult result = (JobResult) context.getJobDetail().getJobDataMap().get("JOB_RESULT");
            result.setFinishedTime(System.currentTimeMillis());
            result.setJobName(detail.getName());
            result.setSuccess(true);
            detail.getJobDataMap().put("JOB_RESULT",result);
            scheduler.scheduleJob(detail, trigger);
            logger.info(nextJobClass.getName() + " has been scheduled executed");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
